package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.message.bean.GroupChatActivityDetail;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/1/6
 **/
public class GroupChatActivityJoinedActivity extends BaseActivityImpl {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    public static void show(Context context, ArrayList<GroupChatActivityDetail.UserInfo> userInfos) {
        Intent intent = new Intent(context, GroupChatActivityJoinedActivity.class);
        intent.putExtra("userInfos", userInfos);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<GroupChatActivityDetail.UserInfo> userInfos = (ArrayList<GroupChatActivityDetail.UserInfo>) getIntent().getSerializableExtra("userInfos");
        setTitleStr("参与活动人员（" + userInfos.size() + "）");

        int dp12 = SizeUtils.dp2px(12);
        recyclerView.setPadding(dp12, 0, dp12, dp12);
        recyclerView.setBackgroundColor(Color.WHITE);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 5));
        recyclerView.setAdapter(new RecyclerView.Adapter() {

            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                LinearLayout linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
                linearLayout.setPadding(0, SizeUtils.sp2px(12), 0, 0);
                int width = ScreenUtils.getScreenWidth() / 10;

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, width);
                layoutParams.setMargins(0, 0, 0, SizeUtils.dp2px(5));
                ImageView iv = new ImageView(mContext);
                iv.setImageResource(R.mipmap.icon_default_header_man);
                linearLayout.addView(iv, layoutParams);

                TextView tv = new TextView(mContext);
                tv.setGravity(Gravity.CENTER_HORIZONTAL);
                tv.setTextAppearance(R.style.NormalTextView);
                tv.setSingleLine(true);
                tv.setEllipsize(TextUtils.TruncateAt.END);
                linearLayout.addView(tv);

                return new ViewHolder(linearLayout);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                ViewHolder viewHolder = (ViewHolder) holder;
                GroupChatActivityDetail.UserInfo userInfo = userInfos.get(position);
                viewHolder.iv.setBackground(null);
                GlideHelper.setHeader(mContext, userInfo.getUser_img(), viewHolder.iv);
                viewHolder.tv.setText(userInfo.getUsername());

                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserHomePageActivity.show(mContext, userInfo.getUser_id(), userInfo.getRoleId());
                    }
                });
            }

            @Override
            public int getItemCount() {
                return userInfos.size();
            }

            class ViewHolder extends RecyclerView.ViewHolder {

                ImageView iv;
                TextView tv;

                ViewHolder(@NonNull View itemView) {
                    super(itemView);
                    LinearLayout linearLayout = (LinearLayout) itemView;
                    iv = (ImageView) linearLayout.getChildAt(0);
                    tv = (TextView) linearLayout.getChildAt(1);
                }
            }
        });
    }
}























